import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views/Home.vue'),
      meta: { title: '首页' }
    },
    {
      path: '/news/:id',
      name: 'NewsDetail',
      component: () => import('@/views/NewsDetail.vue'),
      meta: { title: '新闻详情' }
    },
    {
      path: '/category/:id',
      name: 'CategoryNews',
      component: () => import('@/views/CategoryNews.vue'),
      meta: { title: '分类新闻' }
    },
    {
      path: '/search',
      name: 'SearchResult',
      component: () => import('@/views/SearchResult.vue'),
      meta: { title: '搜索结果' }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/views/NotFound.vue'),
      meta: { title: '页面不存在' }
    }
  ]
})

// 路由守卫 - 设置页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = `${to.meta.title} - 新闻平台`
  }
  next()
})

export default router
